package org.example.service;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.example.entity.Audit;
import org.example.entity.Review;
import org.example.mapper.AuditMapper;
import org.example.mapper.ReviewMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.util.List;

@Service
public class ReviewService extends ServiceImpl<ReviewMapper, Review> {
    @Resource
    ReviewMapper reviewMapper;
    @Resource
    AuditMapper auditMapper;

    public IPage<Review> getReviewWithAudit (Page<Review>page, QueryWrapper<Review> queryWrapper){
        IPage<Review> resultPage=reviewMapper.selectPage(page,queryWrapper);
        List<Review> reviewList = resultPage.getRecords();
        for (Review review : reviewList)
            if (ObjectUtil.isNotEmpty(review.getAuditId())) {
                Audit audit = auditMapper.selectById(review.getAuditId());
                if (ObjectUtil.isNotEmpty(audit)) {
                    audit.setStatus(review.getStatus());
                    audit.setReason(review.getReason());
                    auditMapper.updateById(audit);
                    //实现把两个字段插入到Audit表中去
                    review.setName(audit.getName());
                    review.setUsername(audit.getUsername());
                    review.setProject(audit.getProject());
                    review.setProjectName(audit.getProjectName());
                    review.setPhone(audit.getPhone());
                    review.setAvatar(audit.getAvatar());
                    review.setAnnexes(audit.getAnnexes());
                    review.setUserId(audit.getUserId());
                    reviewMapper.updateById(review);
                }
            }
        return resultPage;
    }

}
